package com.kurancy.web.util;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.kurancy.domain.field.base.NameValuePair;


public class DropdownUtil {
	

	/** The logger. */
	private static Log logger = LogFactory.getLog(DropdownUtil.class);

	/**
	 * Creates a new NameValuePair object.
	 *
	 * @param source the source
	 * @param name the name
	 * @param value the value
	 * @param createBlankRow the create blank row
	 * @return the list< name value pair>
	 */
	public static List<NameValuePair> createNameValuePairsFromEnum(Object[] source, String name, String value, boolean createBlankRow) {
			
			List<NameValuePair> pairs = new ArrayList<NameValuePair>();
			
			if (createBlankRow) {
				pairs.add(new NameValuePair());
			}
			
			try {
				for (int i=0; i<source.length; i++) {
					Object pair = source[i];
					Class<?> c = pair.getClass();
					Method nameMethod = c.getMethod(name);
					Method valueMethod = c.getMethod(value);
					// the name to be added to the NameValuePair
					Object n = nameMethod.invoke(pair);
					// the value to be added to the NameValuePair
					Object v = valueMethod.invoke(pair);
					pairs.add(new NameValuePair(n,v));
				}
				
			} catch (Exception e) {
					logger.error(e.getMessage(), e);
				}
			return pairs;
		}

}
